package sqlSession;

import contant.SqlTypeContant;
import pojo.Configuration;
import pojo.MappedStatement;

import java.lang.reflect.*;
import java.util.List;

public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }


    @Override
    public <E> List<E> selectAll(String statementid, Object... params) throws Exception {

        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementid);
        List<Object> query = (new SimpleExecutor()).query(configuration, mappedStatement, params);
        return (List<E>) query;
    }

    @Override
    public <T> T selectById(String statementid, Object... params) throws Exception {

        List<Object> objects = selectAll(statementid, params);

        if (objects.size() == 1){
            return (T) objects.get(0);
        }else{
            throw new RuntimeException("查询结果不存在或存在多个值");
        }
    }

    @Override
    public int delete(String statementid, Object... params) throws Exception {
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementid);

        return (new SimpleExecutor()).update(configuration, mappedStatement, params);
    }

    @Override
    public int insert(String statementid, Object... params) throws Exception {
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementid);

        return (new SimpleExecutor()).update(configuration, mappedStatement, params);
    }

    @Override
    public int update(String statementid, Object... params) throws Exception {
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementid);

        return (new SimpleExecutor()).update(configuration, mappedStatement, params);
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        Object o = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                //获取方法的名称
                String id = method.getName();
                //获取方法的类的对象
                String namespace = method.getDeclaringClass().getName();

                String statementid = namespace+"."+id;

                //获取method返回的类型
                Type genericReturnType = method.getGenericReturnType();

                //获取mappedStatement
                MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementid);

                //判断是否都update方法
                if (SqlTypeContant.isUpdate(mappedStatement.getSqlType())){
                    return (new SimpleExecutor()).update(configuration, mappedStatement, args);
                }

                //查询方法
                //判断是否实现泛型类型参数化
                if (genericReturnType instanceof ParameterizedType){
                    return selectAll(statementid,args);
                }
                return selectById(statementid,args);
            }
        });
        return (T) o;
    }
}
